<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入店铺" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="用户昵称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入店铺商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态" prop="status">
<!--                <dict-select :options="dict.type.user_type" v-model="queryParams.enable" placeholder="请选择用户类型"/>-->
        <el-select v-model="queryParams.status" placeholder="请选择商品状态">
          <el-option
            v-for="item in goodsState"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="支付方式" prop="payType">
                <dict-select :options="dict.type.pay_type" v-model="queryParams.payType" placeholder="请选择支付方式"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="skuList">
      <el-table-column label="店铺名称" align="center" prop="shopName" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="用户名" align="center" prop="userName" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="商品名称" align="left" prop="goodsName" width="500" :show-overflow-tooltip="true"/>
      <el-table-column label="进货单价" align="center" prop="costPrice" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ formatter(scope.row, 'costPrice') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出售单价" align="center" prop="price" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ formatter(scope.row, 'price') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主播带货量" align="center" prop="anchorNum" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="虚拟销量" align="center" prop="navyNum" width="120" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="总货物量" align="center" prop="" width="120" :show-overflow-tooltip="true"/>-->
      <el-table-column label="剩余货物" align="center" prop="num" width="120" :show-overflow-tooltip="true">
      <template slot-scope="scope">
        <div>{{ scope.row.num }}</div>
<!--        <el-button @click="editInventory(scope.row)" size="mini" type="text">修改</el-button>-->
      </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="支付状态" align="center" prop="state" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>-->
      <el-table-column label="上/下架" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="true"
                     :inactive-value="false" @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="总价" align="center" prop="totalPrice" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ formatter(scope.row, 'totalPrice') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{m}/{d}/{y}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
                @pagination="getList"
    />
  </div>
</template>

<script>
import { listSku, changeStatus } from '@/api/store/storeProduct'
import { changeBalance } from '@/api/system/user'

export default {
  name: 'Sku',
  dicts: ['pay_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // sku库存表格数据
      skuList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        shopId:null,
      },
      goodsState:[{label:'上架',value:1},{label:'下架',value:0}],
    }
  },
  created() {
    if(this.$route.query.shopId){
      this.queryParams.shopId = this.$route.query.shopId;
    }
    this.getList()
  },
  methods: {
    /** 查询sku库存列表 */
    getList() {
      this.loading = true
      let setData = {
        ...this.queryParams
      }
      listSku(setData).then(response => {
        this.skuList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm('queryForm')
      this.queryParams ={
        current: 1,
          size: 10
      };
      // this.handleQuery()
      this.getList()
    },
    /** 修改状态 */
    statusChange(row) {
      changeStatus(row.id).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.getList()
      })
    },
    //格式化金额
    formatter: function(row, key) {
      return this.$yun.priceFormatter(row[key])
    },
    //修改库存
    editInventory({id}) {
      this.$prompt(null, '修改库存', {
        confirmButtonText: this.$t('ok'),
        cancelButtonText: this.$t('cancel'),
        closeOnClickModal: false,
        inputType: 'el-input-number',
        inputPattern: /^[+]?(0|([1-9]\d*))(\.\d+)?$/,
        inputErrorMessage: "请输入合法的余额库存量"
      }).then(async ({value}) => {
        // await changeBalance(id,value)
        // todo 修改接口
        this.$modal.msgSuccess("修改成功");
        this.getList()
      }).catch(() => {
      });
    },
  }
}
</script>
